package controller

import (
	"store/serializer"
	"store/service"

	"github.com/cihub/seelog"
	"github.com/gin-gonic/gin"
)

// FileUpload 处理文件上传
func FileUpload(c *gin.Context) {
	file, err := c.FormFile("upload")
	if nil != err {
		seelog.Errorf("获取文件失败:%s", err.Error())
		serializer.ResponseServerErr(c, "获取文件失败")
		return
	}
	fileStoreService := service.FileStoreService{}
	fileID, err := fileStoreService.Upload(file, c.ClientIP())
	if nil == err {
		serializer.ResponseOkWithData(c, fileID)
	} else {
		seelog.Errorf("处理文件失败:%s", err.Error())
		serializer.ResponseServerErr(c, "处理文件失败")
	}
}

// FileDownload 处理文件下载
func FileDownload(c *gin.Context) {
	fileID := c.Query("fileID")
	fileStoreService := service.FileStoreService{}
	filePath, filename, err := fileStoreService.Get(fileID)
	if nil == err {
		c.FileAttachment(filePath, filename)
	} else {
		serializer.ResponseParamErr(c)
	}
}

// FileDelete 处理文件删除
func FileDelete(c *gin.Context) {
	fileID := c.Query("fileID")
	fileStoreService := service.FileStoreService{}
	fileStoreService.Delete(fileID)
	serializer.ResponseOk(c)
}
